const path_ = eval('require')('path');
const fs = eval('require')('fs');

class copyDirAsync {
    constructor() {
        this.tasks = [
            // fs.copyFile(from, to, err => {
            //     dear err
            // }),
        ];
        this.cb = () => {};
        this.baseCB = (function (err) {
            if (err) {
                this.cb({ err });
            } else if (this.tasks.length) {
                this.cb({
                    data: this.tasks.length,
                    status: 'copying',
                });
                this.tasks.pop()();
            } else {
                this.cb({ status: 'over' });
            }
        }).bind(this);
    }

    walkAndCreateDir(from, to) {
        fs.readdirSync(from)
            .forEach((f) => {
                const fPath = path_.join(from, f);
                const tPath = path_.join(to, f);
                if (fs.statSync(fPath).isDirectory()) {
                    if (!fs.existsSync(tPath)) {
                        fs.mkdirSync(tPath);
                    }
                    this.walkAndCreateDir(fPath, tPath);
                } else {
                    this.addTask(fPath, tPath);
                }
            });
    }

    init(src, dist, withPath) {
        if (!fs.existsSync(dist)) {
            fs.mkdirSync(dist);
        }
        if (withPath) {
            dist = path_.join(dist, path_.basename(src));
            if (!fs.existsSync(dist)) {
                fs.mkdirSync(dist);
            }
        }
        this.walkAndCreateDir(src, dist);
        this.run();
    }

    addTask(from, to) {
        this.tasks.push(
            fs.copyFile.bind(fs, from, to, this.baseCB),
        );
    }

    run() {
        this.baseCB(null);
    }
}

module.exports = {
    sync(src, dist, withPath) {
        function copyFolder(from, to) {        // 复制文件夹到指定目录
            let files = [];
            if (fs.existsSync(to)) {           // 文件是否存在 如果不存在则创建
                files = fs.readdirSync(from);
                files.forEach((file) => {
                    const targetPath = path_.join(from, file);
                    const toPath = path_.join(to, file);
                    if (fs.statSync(targetPath).isDirectory()) { // 复制文件夹
                        copyFolder(targetPath, toPath);
                    } else {                                    // 拷贝文件
                        fs.copyFileSync(targetPath, toPath);
                    }
                });
            } else {
                fs.mkdirSync(to);
                copyFolder(from, to);
            }
        }
        if (!fs.existsSync(dist)) {
            fs.mkdirSync(dist);
        }
        if (withPath) {
            dist = path_.join(dist, path_.basename(src));
            if (!fs.existsSync(dist)) {
                fs.mkdirSync(dist);
            }
        }
        copyFolder(path_.join(src), dist);
    },
    async(cb, src, dist, withPath) {
        const cda = new copyDirAsync();
        cda.cb = cb;
        cda.init(src, dist, withPath);
    },
};
